package fr.ants.controller.command;

import fr.ants.controller.clock.Clock;
import fr.ants.model.Resource;
import fr.ants.view.panel.Canvas;
import fr.ants.view.panel.TabbedPane;

/**
 * Command to add a new Resource.
 * 
 * @author Jordan ENSFELDER
 * 
 */
public class NewResourceCommand implements Command {

	/**
	 * Canvas.
	 */
	private final Canvas canvas;

	/**
	 * Clock.
	 */
	private final Clock clock;

	/**
	 * TabbedPane.
	 */
	private TabbedPane tabbedPane;

	/**
	 * Build.
	 * 
	 * @param canvas
	 *            Canvas.
	 * @param clock
	 *            Clock.
	 */
	public NewResourceCommand(final Canvas canvas, final Clock clock) {
		this.canvas = canvas;
		this.clock = clock;
	}

	/**
	 * we need the tabbedpane to execute this command.
	 * 
	 * @param tabbedPane
	 *            tabbedpane
	 */
	public void setTabbedPane(final TabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}

	@Override
	public void execute() {

		if (canvas.getSelectedGridX() != null
				&& canvas.getSelectedGridY() != null) {
			Resource resource = new Resource(tabbedPane.getSimulationPanel()
					.getCapacityValue(), tabbedPane.getSimulationPanel()
					.getSpeedValue());
			int pSize = tabbedPane.getSimulationPanel().getSizeValue();

			if (resource != null) {
				clock.getSimulator()
						.getGrid()
						.addResource(canvas.getSelectedGridX(),
								canvas.getSelectedGridY(), resource, pSize);
			}

			if (!clock.isRunning()) {
				canvas.repaint();
			}
		}

	}

}
